﻿package plugins.mstandio.utils.combobox {
	
	import flash.display.Sprite;
	import flash.display.BlendMode;
	import flash.text.TextField;	
	import flash.text.TextFormat;	
	import flash.text.TextFormatAlign;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;
	
	import plugins.mstandio.utils.combobox.ComboBoxStyle;
	
	/**
	 * Defines single element on combobox list that stores given data
	 * @author mstandio
	 */
	public class ComboBoxElement extends Sprite{
		
		public var active:Boolean; // if its label is currently in main element				
		private var enabled:Boolean; // if it is interactive
		
		public var object:Object;		
		
		private var txt:TextField;
		private var txtFormat:TextFormat;				
		private var style:ComboBoxStyle;		
		
		public function ComboBoxElement(object:Object) {			
			this.object = object;
			
			this.active = false; // this means it wont be highlited after mouse over
			this.enabled = true; // it is interactive			
			
			this.txt = new TextField();
			this.txtFormat = new TextFormat();			
			this.addChild(txt);					
			
			this.txtFormat.blockIndent = 0;
			this.txt.selectable = false;								
			this.txt.blendMode = BlendMode.LAYER;
			//this.txt.cacheAsBitmap = true;

			this.txt.text = this.object.label;
			
			this.buttonMode = true;
			this.useHandCursor = true;
			this.mouseChildren = false;
									
			this.addEventListener(MouseEvent.MOUSE_OVER, over);
			this.addEventListener(MouseEvent.MOUSE_OUT, out);
			this.addEventListener(MouseEvent.CLICK, click);	
		}		
		
		public function setStyle(style:ComboBoxStyle):void{		
			this.style = style;			
			
			this.txtFormat.font = this.style.fontFamily;
			this.txtFormat.size = this.style.fontSize;			
			this.txtFormat.color = this.style.fontColor; 
			this.txtFormat.bold = this.style.fontBold;
			this.txtFormat.italic = this.style.fontItalic;																				
			this.txtFormat.leftMargin = this.style.marginLeft;
			this.txtFormat.rightMargin = this.style.marginRight;			
			
			this.txt.background = true;									
			this.txt.backgroundColor = this.style.elementBackgroundColor;			
			this.txt.border = this.style.elementBorder;
			this.txt.borderColor = this.style.elementBorderColor;			
			this.txt.height = (this.style.elementHeight != -1) ? this.style.elementHeight : this.style.fontSize * 1.4; 			
			this.alpha = this.style.elementAlpha;
			
			this.refresh();
		}						
		
		public function setWidth(elementWidth:Number):void {			
			this.txt.width = (this.style.elementWidth != -1) ?  this.style.elementWidth : elementWidth;					
		}				
		
		public function setMainElement():void {
			this.alpha = this.style.mainElementAlpha
			this.txtFormat.color = this.style.mainElementFontColor;									
			this.txt.border = this.style.mainElementBorder;						
			if (this.style.initiallyBlank) {
				this.txt.text = "";
			}
			this.txt.setTextFormat(this.txtFormat);	
		}
		
		private function over(e:Event):void {
			if(!this.active){
				this.setSelectedBackground(true);
			}
		}
				
		private function out(e:Event):void {
			if(!this.active){
				this.setSelectedBackground(false);
			}
		}
		
		private function click(e:Event):void {						
			if((this.parent) is getClass(ComboBox)){
				(ComboBox(this.parent)).setActive(this.object);					
			}else if (this.parent.parent is this.getClass(ComboBox)) {
				(ComboBox(this.parent.parent)).setActive(this.object);					
			}			
		}						
		
		public function setSelectedBackground(command:Boolean):void {
			if (command && this.enabled) {
				this.txt.backgroundColor = this.style.elementSelectedColor;
			}else {
				this.txt.backgroundColor = this.style.elementBackgroundColor;
			}
		}				
		
		public function setEnabled(changeFontColor:Boolean=true):void {						
			this.enabled = true;
			if(changeFontColor){
				this.txtFormat.color = this.style.fontColor; 						
				this.refresh();
			}
			this.buttonMode = true;
			this.useHandCursor = true;
		}	
		
		public function setDisabled(changeFontColor:Boolean=true):void {			
			this.enabled = false;
			if(changeFontColor){
				this.txtFormat.color = 0xdddddd; 			
				this.refresh();
			}
			this.buttonMode = false;
			this.useHandCursor = false;
		}
		
		public function refresh():void {
			this.txt.text = this.object.label			
			this.txt.setTextFormat(this.txtFormat);			
		}				
		
		public function getTextWidth():Number {
			return this.txt.textWidth;
		}
		
		private function getClass(obj:Object):Class {
			return Class(getDefinitionByName(getQualifiedClassName(obj)));
		}
	}	
}